#-*- coding: utf-8 -*-
#! /usr/bin/python

import hashlib
import datetime
import time
import sys
import ConfigParser

reload(sys)
sys.setdefaultencoding("utf-8")


def initConfig(filename):
    '''
    去掉文件头BOM等
    Window下用记事本打开配置文件并修改保存后，编码为UNICODE
    或UTF-8的文件的文件头
    #会被相应的加上\xff\xfe（\xff\xfe）或\xef\xbb\xbf
    '''
    import re
    content = open(filename).read()
    content = re.sub(r"\xfe\xff", "", content)
    content = re.sub(r"\xff\xfe", "", content)
    content = re.sub(r"\xef\xbb\xbf", "", content)
    open(filename, 'w').write(content)


def logit(content, filename='makesnlog.txt'):
    '''
    逐行添加日志，每行一个回车
    头部是时间
    '''
    file = open(filename, 'a')
    try:
        timestr = datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S')+': '
        file.write(timestr+content+'\r\n')
    finally:
        file.close()


def makesn(soft, MAC):
    '''
    soft是软件的发布版exe名字
    不绑定小号大号，只限制电脑
    '''
    soft = soft + MAC
    x = hashlib.sha224((hashlib.md5('jhf')).hexdigest()+'jhfaixiaobao'+soft)
    return x.hexdigest()

def getMacAdd():
    '''
    获得mac地址
    '''
    import uuid
    node = uuid.getnode()
    mac = uuid.UUID(int = node).hex[-12:]
    return mac



#读取配置文件
config = ConfigParser.ConfigParser()
initConfig('sn.ini')
config.read('sn.ini')
soft = config.get("global", "exe")
#MAC = config.get("global", "mac")
MAC = getMacAdd()

sn = makesn(soft, MAC)
logit('Mac is '+MAC+', SN is '+sn)
print 'Got the SN: '+sn
time.sleep(6)
print 'OK,type Enter key to Exit...'
s = raw_input()
if(s):
    print 'Bye~!'
    exit()
